#include "mini_crt.h"

extern int main(int argc, char* argv[]);
void mini_crt_entry(void) {
	int ret = 0;
	int argc;
	char** argv = 0;
	char* bp_reg = 0;


#ifdef M64
	asm("movq %%rbp, %0\n":"=r"(bp_reg));
#else
	asm("mov %%ebp, %0\n":"=r"(bp_reg));
#endif
	argc = *(int*)(bp_reg + sizeof(void*));
	argv = (char**)(bp_reg + 2 * sizeof(void*));
	
	debug_init(argv);
	if(mini_crt_heap_init()) {
	    perror("heap init failed!\n");
		goto exit_tag;
	}
	
	if(mini_crt_io_init()) {
	    perror("io init failed!\n");
		goto exit_tag;
	}
	
	ret = main(argc, argv);
	
exit_tag:
	exit(ret);	
}
